#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : interface.accelerate.py
# @Note    : "仿真加速"窗口界面
import os

import psutil
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QSlider, QMessageBox
from PyQt5.uic import loadUiType

ui, _ = loadUiType("./resource/ui/accelerate.ui")


# 联系与反馈信息界面
class accelerateDialog(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.setWindowIcon(QIcon("../resource/icon/about.png"))
        self.setWindowTitle("仿真加速")
        self.setFixedSize(self.width(), self.height())
        self.horizontalSlider_sim_speed.setTickPosition(QSlider.TicksBelow)  # 设置刻度位置，在下方
        self.horizontalSlider_sim_speed.setTickInterval(1)  # 设置刻度间隔
        self.horizontalSlider_sim_resolution.setTickPosition(QSlider.TicksBelow)
        self.horizontalSlider_sim_resolution.setTickInterval(1)
        self.horizontalSlider_control_freq.setTickPosition(QSlider.TicksBelow)
        self.horizontalSlider_control_freq.setTickInterval(1)
        # 槽连接
        self.horizontalSlider_sim_speed.valueChanged.connect(self.slider_sim_speed_valChange)
        self.horizontalSlider_sim_resolution.valueChanged.connect(self.slider_sim_resolution_valChange)
        self.horizontalSlider_control_freq.valueChanged.connect(self.slider_control_freq_valChange)
        self.pushButton_power_ok.clicked.connect(self.choose_power_mode)
        self.pushButton_accelerate.clicked.connect(self.accelerate_set)
        # 定时器
        # self.cpu_timer = QTimer(self)
        # self.cpu_timer.start(1000)
        # self.cpu_timer.timeout.connect(self.update_cpu_status)

    def slider_sim_speed_valChange(self):
        value = self.horizontalSlider_sim_speed.value()
        self.lineEdit_sim_speed.setText(str(value))

    def slider_sim_resolution_valChange(self):
        value = self.horizontalSlider_sim_resolution.value()
        self.lineEdit_sim_resolution.setText(str(value))

    def slider_control_freq_valChange(self):
        value = self.horizontalSlider_control_freq.value()
        self.lineEdit_control_freq.setText(str(value))

    def choose_power_mode(self):
        idx = self.comboBox_power_mode.currentIndex()
        self.power_config(idx)

    # CPU电源模式配置(开启高性能模式)
    def power_config(self, index=2):
        name = ["节能模式", "平衡模式", "高性能模式", "卓越模式"]
        mode = [
            "a1841308-3541-4fab-bc81-f71556f20b4a",  # 节能
            "381b4222-f694-41f0-9685-ff5bb260df2e",  # 平衡
            "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c",  # 高性能
            "e9a42b02-d5df-448d-aa00-03f14749eb61",  # 卓越
        ]
        cmd = "Powercfg -s %s" % mode[index]
        flag = os.system(cmd)
        if flag == 1:
            os.system("powercfg -duplicatescheme %s" % mode[index])
            os.system(cmd)
            if flag == 1:
                if index != 1:
                    QMessageBox.information(self, "提示", "无法启用【%s】，已自动为您切换为【平衡】" % name[index])
                    self.power_config(1)
        else:
            QMessageBox.information(self, "提示", "启用【%s】成功" % name[index])

    def update_cpu_status(self):
        rate = psutil.cpu_percent(3)
        self.progressBar_cpu_use_rate.setValue(int(rate))

    def graphics_set(self):
        if self.radioButton_hidden_flow.isChecked():
            visualization = False
        else:
            visualization = True
        if self.radioButton_3D_mode.isChecked():
            mode = True
        else:
            mode = False
        return visualization, mode

    def accelerate_set(self):
        self.comboBox_power_mode.setCurrentIndex(2)
        self.power_config(2)
        self.horizontalSlider_sim_speed.setValue(10)
        self.horizontalSlider_sim_resolution.setValue(1)
        self.horizontalSlider_control_freq.setValue(1)
        self.radioButton_hidden_flow.setChecked(True)
        self.radioButton_2D_mode.setChecked(True)
